from django.db import models


class B2b(models.Model):
    b2b_name = models.CharField(max_length=100)


class ProductsLink(models.Model):
    entity_id = models.IntegerField(primary_key=True)
    sku = models.CharField(max_length=100)
    name = models.CharField(max_length=500, null=True)


class B2b_Category(models.Model):
    b2b_id = models.ForeignKey(B2b)
    category = models.CharField(max_length=1000)
    new = models.NullBooleanField()
    delete = models.NullBooleanField()


class B2b_Price(models.Model):
    product = models.ForeignKey(ProductsLink)
    b2b = models.ForeignKey(B2b)
    b2b_name = models.CharField(max_length=200, null=True)
    last_update = models.DateTimeField(null=True)
    category = models.CharField(max_length=200, null=True)
    b2b_category = models.ForeignKey(B2b_Category, null=True)
    avail = models.NullBooleanField()
    price_rule = models.CharField(max_length=100, null=True)
    price_current = models.DecimalField(max_digits=10, decimal_places=4, null=True)
    change_avail = models.NullBooleanField()
    change_price = models.NullBooleanField()


class B2b_Price_Rule(models.Model):
    b2b_id = models.ForeignKey(B2b)
    category_name = models.CharField(max_length=500)
    price_level = models.IntegerField(null=True)
    price_additionally = models.DecimalField(max_digits=10, decimal_places=4, null=True)


class BaseProduct(models.Model):
    entity_id = models.IntegerField(null=True)
    sku = models.CharField(max_length=100, primary_key=True)
    name = models.CharField(max_length=100, null=True)
    group = models.CharField(max_length=100, null=True)
    brand_name = models.CharField(max_length=100, null=True)
    description = models.CharField(max_length=1000, null=True)
    short_description = models.CharField(max_length=500, null=True)

    def __unicode__(self):
        return self.sku

    class Meta():
        db_table = 'export_product'


class BaseProduct_B2b(models.Model):
    b2b = models.CharField(max_length=100)

    def __unicode__(self):
        return self.b2b

    class Meta():
        db_table = 'export_b2b'


class BaseProduct_Stock(models.Model):
    stock_name = models.CharField(max_length=100)
    b2b = models.ForeignKey(BaseProduct_B2b)

    def __unicode__(self):
        return self.stock_name

    class Meta():
        db_table = 'export_stock'


class BaseProduct_Attr(models.Model):
    attr_name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.attr_name

    class Meta():
        db_table = 'export_product_attr'


class BaseProduct_Attr_Value(models.Model):
    attr = models.IntegerField()
    sku = models.CharField(max_length=100)
    attr_value = models.CharField(max_length=500)

    def __unicode__(self):
        return self.attr

    class Meta():
        db_table = 'export_product_attr_value'


class BaseProduct_Price(models.Model):
    sku = models.ForeignKey(BaseProduct)
    b2b = models.ForeignKey(BaseProduct_B2b)
    price_usd = models.DecimalField(max_digits=10, decimal_places=2)
    price_uah = models.DecimalField(max_digits=10, decimal_places=2)

    def __unicode__(self):
        return "%s:%s/%s" % (self.sku, self.price_uah, self.price_usd)

    class Meta():
        db_table = 'export_product_price'
        ordering = ['price_uah']


class BaseProduct_Sku(models.Model):
    product = models.ForeignKey(BaseProduct)
    b2b = models.ForeignKey(BaseProduct_B2b)
    sku_b2b = models.CharField(max_length=100)

    def __unicode__(self):
        return self.sku_b2b

    class Meta():
        db_table = 'export_product_sku'


class BaseProduct_Stock_Availability(models.Model):
    stock_id = models.ForeignKey(BaseProduct_Stock)
    product_id = models.ForeignKey(BaseProduct)
    availability = models.CharField(max_length=50)

    def __unicode__(self):
        return '%s/%s - %s', (self.stock_id, self.product_id, self.availability)

    class Meta():
        db_table = 'export_stock_availability'


class Currency(models.Model):
    currency = models.DecimalField(max_digits=10, decimal_places=4)


